# -*- coding: utf-8 -*-
'''
Author: your name
Date: 2021-02-01 17:58:47
LastEditTime: 2021-02-02 09:22:41
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /职级爬虫脚本/nowcoder.py
'''

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

from pprint import pprint
import json
import requests

# reload(sys)
# sys.setdefaultencoding('utf8')

# "https://m.nowcoder.com/nccommon/offer/show-v2/newest-filter?offset=0&limit=10&type=2"


def get_url(offset, limit, type):
    return "https://m.nowcoder.com/nccommon/offer/show-v2/newest-filter?offset="+str(offset)+"&limit="+str(limit)+"&type="+str(type)


def get_data(url):
    r = requests.get(url)
    json_tmp = r.json()
    json_str = json.dumps(json_tmp)
    user_dic = json.loads(json_str)
    return user_dic


for i in range(10000):
    newestOffers = get_data(get_url(i*100, 100, 2))["data"]["newestOffers"]
    for i in newestOffers:
        var1 = str(i["occupation"])
        var2 = str(i["city"])
        var3 = str(i["year"])
        var4 = str(i["schoolType"])
        var5 = str(i["statusOperatorUid"])
        var6 = str(i["likeCount"])
        var7 = str(i["salary"])
        var8 = str(i["type"])
        var9 = str(i["salaryMonthCount"])
        var10 = str(i["uid"])
        var11 = str(i["educationLevel"])
        var12 = str(i["occupationId"])
        var13 = str(i["salaryPerMonth"])
        var14 = str(i["company"])
        var15 = str(i["id"])
        # var16 = str(i["department"])
        var17 = str(i["disLikeCount"])
        var18 = str(i["occupationComment"])
        var19 = str(i["level"])
        var20 = str(i["isLiked"])
        var21 = str(i["updateTime"])
        var22 = str(i["commentUserCount"])
        var23 = str(i["isDisLiked"])
        var24 = str(i["companyId"])
        var25 = str(i["createTime"])
        var26 = str(i["comment"])
        var27 = str(i["mergeLikeCount"])
        var28 = str(i["status"])
        split_char = "@"
        f = open('data_nowcoder.txt', 'a+')
        f.writelines(var1+split_char +
                     var2+split_char +
                     var3+split_char +
                     var4+split_char +
                     var5+split_char +
                     var6+split_char +
                     var7+split_char +
                     var8+split_char +
                     var9+split_char +
                     var10+split_char +
                     var11+split_char +
                     var12+split_char +
                     var13+split_char +
                     var14+split_char +
                     var15+split_char +
                     #  var16+split_char +
                     var17+split_char +
                     var18+split_char +
                     var19+split_char +
                     var20+split_char +
                     var21+split_char +
                     var22+split_char +
                     var23+split_char +
                     var24+split_char +
                     var25+split_char +
                     var26+split_char +
                     var27+split_char +
                     var28+split_char+"\n")
        f.close()
